package com.rainsoft.center.isec.frame.entity;

import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.configuration.tree.ConfigurationNode;

import java.io.Serializable;
import java.util.List;

/**
 * @Package com.rainsoft.center.isec.frame
 * @Description:
 * @Author elwyn
 * @Date 2017/12/10 22:57
 * @Email elonyong@163.com
 */
public class AppConfig implements Serializable{
    private String appName;
    private String jobClassName;
    private String entityClassName;
    private Long durations;
    private String hTableName;
    private String family;
    private String topic;

    private AppConfig() {

    }

    public String getAppName() {
        return appName;
    }


    public String getJobClassName() {
        return jobClassName;
    }


    public String getEntityClassName() {
        return entityClassName;
    }

    public Long getDurations() {
        return durations;
    }


    public String gethTableName() {
        return hTableName;
    }


    public String getFamily() {
        return family;
    }

    public String getTopic() {
        return topic;
    }

    public static AppConfig getAppConfig(String appName) {
        try {
            AppConfig appConfig = new AppConfig();
            XMLConfiguration xmlConfiguration = new XMLConfiguration("appConf.xml");
            for (Object o : xmlConfiguration.getRoot().getChildren()) {
                ConfigurationNode next = (ConfigurationNode) o;
                List<ConfigurationNode> children = next.getChildren();
                if (children.get(0).getValue().equals(appName)) {
                    appConfig.durations = Long.valueOf(String.valueOf(children.get(1).getValue()));
                    appConfig.jobClassName = String.valueOf(children.get(2).getValue());
                    appConfig.entityClassName = String.valueOf(children.get(3).getValue());
                    appConfig.hTableName = String.valueOf(children.get(4).getValue());
                    appConfig.family = String.valueOf(children.get(5).getValue());
                    appConfig.topic = String.valueOf(children.get(6).getValue());
                    break;
                }
            }
            appConfig.appName = appName;
            return appConfig;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
}
